#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_rcc.h"
#include "stdio.h"
#include "log.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DevicesPower.h"

#include "DriverCurrent.h"

#include "taskCurrent.h"


/* 电流监控任务句柄 */
TaskHandle_t g_TaskCurrentHand = NULL;


void vTaskCurrent(void *pvParameters)
{
    uint32_t uiNotifiedValue = 0;
    CurrentInfoType *ptypeCurrentInfoHandle = ptypeCurrentInfoGet();

    while(1)
    {
        xTaskNotifyWait(0x00000000, 0xFFFFFFFF, &uiNotifiedValue, 1 / portTICK_RATE_MS);

        /* 需要实时更新数据 */
        vCurrentUpdate();

        /* 监控各通道的电流是否超过限制值 */
        if(ptypeCurrentInfoHandle->vdd1.get > ptypeCurrentInfoHandle->vdd1.limit)
        {
            vPowerClose(POWER_CHANNEL_VCC1);
            ptypeCurrentInfoHandle->vdd1.error = ptypeCurrentInfoHandle->vdd1.get;
        }

        if(ptypeCurrentInfoHandle->vdd2H.get > ptypeCurrentInfoHandle->vdd2H.limit)
        {
            vPowerClose(POWER_CHANNEL_VCC2H);
            ptypeCurrentInfoHandle->vdd2H.error = ptypeCurrentInfoHandle->vdd2H.get;
        }

        if(ptypeCurrentInfoHandle->vdd2L.get > ptypeCurrentInfoHandle->vdd2L.limit)
        {
            vPowerClose(POWER_CHANNEL_VCC2L);
            ptypeCurrentInfoHandle->vdd2L.error = ptypeCurrentInfoHandle->vdd2L.get;
        }

        if(ptypeCurrentInfoHandle->vddQ.get > ptypeCurrentInfoHandle->vddQ.limit)
        {
            vPowerClose(POWER_CHANNEL_VCCQ);
            ptypeCurrentInfoHandle->vddQ.error = ptypeCurrentInfoHandle->vddQ.get;
        }
    }
}
